﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Radiation___IM.ApiCommands
{
    public class CreateAccount : ICommand
    {
        private readonly string m_commandkey = Radiation.Common.Communication.APIMethods.CREATE_ACCOUNT;

        public CreateAccount()
        {
        }

        public string commandKey()
        {
            return m_commandkey;
        }

        public string requiredPermission()
        {
            return "register";
        }

        public void execute(string[] commands, Client client)
        {
            if (commands.Length > 3 &&
                                commands[2].Length > 1 &&
                                commands[3].Length > 0)
            {
                if (Users.UserManager.Instance[commands[2]] == null)
                {
                    Users.User user = new Users.User();
                    user.Nickname = commands[2];
                    user.Password = commands[1];
                    Users.UserManager.Instance.addUser(user);

                    MessageExecution.sendDataAPI(client,
                        Radiation.Common.Communication.Constants.ACCOUNT_CREATION_SUCESS,
                        commands[1]);
                }
                else
                    MessageExecution.sendDataAPI(client,
                        Radiation.Common.Communication.Constants.ACCOUNT_CREATION_ALREADY_EXISTS,
                        commands[1]);
            }
            else
                MessageExecution.sendDataAPI(client,
                    Radiation.Common.Communication.Constants.ACCOUNT_CREATION_ERROR,
                    commands[1]);
        }
    }
}
